import { createRouter, createWebHistory } from 'vue-router'
import axios from "axios";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path:'/',
            redirect:'/admin/login'
        },
        {
            path: '/admin/login',
            component: () => import('@/view/admin/AdminLogin.vue')
        },
        {
            path: '/admin/home',
            name: 'AdminHome',
            component: () => import('@/view/admin/AdminHome.vue'),
            children:[
                {
                    path: '/admin/home/order/list',
                    name: 'AdminOrderList',
                    component: () => import('@/view/admin/AdminOrderList.vue')
                },
                {
                    path: '/admin/home/category/list',
                    name: 'AdminCategoryList',
                    component: () => import('@/view/admin/AdminCategoryList.vue')
                },
                {
                    path: '/admin/home/product/list',
                    name: 'AdminProductList',
                    component: () => import('@/view/admin/AdminProductList.vue')
                },
                {
                    path: '/admin/home/activity/list',
                    name: 'AdminActivityList',
                    component: () => import('@/view/admin/AdminActivityList.vue')
                },
                {
                    path: '/admin/home/user/list',
                    name: 'AdminUserList',
                    component: () => import('@/view/admin/AdminUserList.vue')
                },
                {
                    path: '/admin/home/address/list',
                    name: 'AdminAddressList',
                    component: () => import('@/view/admin/MapContainer.vue')
                },
                {
                    path: '/admin/home/日志/list',
                    name: '日志',
                    component: () => import('@/view/admin/日志.vue')
                },
                {
                    path: '/admin/home/coupon/list',
                    name: 'coupon',
                    component: () => import('@/view/admin/coupon.vue')
                }
            ]
        },
    ]
})
router.beforeEach((to, from, next) => {
    if (to.path.startsWith('/admin')) {
        //如果是登录路由则直接放行
        if (to.path == '/admin/login') return next()
        //从sessionStorage取得token代表jwt长串
        var adminToken = sessionStorage.getItem("adminToken");
        if (!adminToken) {//token不存在,代表未登录
            return next('/admin/login')//跳转到登录页面
        } else {//token存在,代表登录
            //检验jwt的合法性
            axios(
                {
                url: 'http://192.168.90.26:8080/admin/checkToken',
                method: 'get',
                headers: {
                    token: adminToken //请求头加token:jwt长串
                }
            }
            ).then(resp => {
                if (resp.data==='验证成功') {
                    next()//放行
                }else if (resp.data==='令牌失效!'){
                    alert('检测token失败,请重新登录')
                    sessionStorage.clear()
                    router.push('/admin/login')
                }else {
                    sessionStorage.setItem("adminToken",resp.data)
                    next()
                }
            })
        }
    }
})
export default router